package com.af.monitorModule.controller.robot;

import com.af.monitorModule.common.ResultEnum;
import com.af.monitorModule.generator.entity.Robot;
import com.af.monitorModule.generator.service.RobotService;
import com.af.monitorModule.util.RestUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;


@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/robot")
public class RobotController {

    @Autowired
    private RobotService robotService;

    /**
     * 获取机器人列表
     * @param page
     * @param limit
     * @param search
     * @return
     */
    @GetMapping("/findRobot")
    public JSONObject findMonitor(String page, String limit, String search){
        return robotService.findRobot(page,limit,search);
    }

    /**
     * 根据机器人ID获取详细信息
     * @param id
     * @return
     */
    @GetMapping("/findRobotById/{id}")
    public JSONObject findRobotById(@PathVariable String id){
        JSONObject res= ResultEnum.OK.getResult();
        Robot byId = robotService.getById(id);
        res.put("data",byId);
        res.put("alive", RestUtil.checkUrl(byId.getRobotRequest(),2,"GET"));
        return res;
    }

    /**
     * 修改机器人内容
     * @param robot
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public JSONObject update(@RequestBody Robot robot, @PathVariable String id){
        return robotService.updateRobot(robot,id);
    }

    /**
     * 添加
     * @param robot
     * @return
     */
    @PostMapping("/add")
    public JSONObject add(@RequestBody Robot robot){
        return robotService.add(robot);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public JSONObject add(@PathVariable String id){
        return robotService.removeById(id) ? ResultEnum.OK.getResult(): ResultEnum.DATA_ISNOT_EXIST.getResult();
    }

    /**
     * 检查某个机器人的响应状态
     * @param id
     * @return
     */
    @GetMapping("/checkOne/{id}")
    public JSONObject checkOne(@PathVariable String id){
        Robot byId = robotService.getById(id);
        if (byId == null) return ResultEnum.DATA_ISNOT_EXIST.getResult();
        JSONObject res = ResultEnum.OK.getResult();
        res.put("alive",RestUtil.checkUrl(byId.getRobotRequest(),2,"GET"));
        return res;
    }

    @GetMapping("/getSysInfo/{id}")
    public JSONObject getSysInfo(@PathVariable String id){
        Robot byId = robotService.getById(id);
        if (byId == null) return ResultEnum.DATA_ISNOT_EXIST.getResult();
        JSONObject res = ResultEnum.OK.getResult();
        res.put("sysInfo",RestUtil.sendGet(byId.getRobotRequest() + "/service/sysInfo",JSONObject.class,new HashMap<>()));
        return res;
    }

    @GetMapping("/getDirConfig/{id}")
    public JSONObject getDirConfig(@PathVariable String id){
        Robot byId = robotService.getById(id);
        if (byId == null) return ResultEnum.DATA_ISNOT_EXIST.getResult();
        JSONObject res = ResultEnum.OK.getResult();
        res.put("dirInfo",RestUtil.sendGet(byId.getRobotRequest() + "/service/getDirConfig",JSONObject.class,new HashMap<>()));
        return res;
    }
}
